import {otherRouter, appRouter} from '../../router/router';
import Util from '../../libs/util';
import common_app from '@/common/store/app';

const app = {
    state: {
        dicTree: {},
        menuList: [],
        currentPath: [
            {
                title: '首页',
                path: '/home',
                name: 'home_index'
            }
        ], // 面包屑数组
        routers: [
            otherRouter,
            ...appRouter
        ]
    },
    mutations: {
        updateMenulist (state, user) {
            let menuList = [];
            var permissions = user.permission;
            var role_type = user.role_type;
            appRouter.forEach((item, index) => {
                if (item.permission && !Util.showThisRoute(item.permission, permissions)) return;
                if(item.role_type && item.role_type.findIndex(i => { return i == role_type }) == -1) return;
                if (item.children && item.children.length > 0) {
                    var childrenArr = [];
                    childrenArr = item.children.filter(child => {
                        if (child.permission !== undefined && !Util.showThisRoute(child.permission, permissions)) return;
                        if(child.role_type && child.role_type.findIndex(i => { return i == role_type }) == -1) return;
                        return child;
                    });
                    if(childrenArr.length > 0) {
                        item.children = childrenArr;
                        menuList.push(item);
                    }
                }
            });
            state.menuList = menuList;
        },
        setCurrentPath (state, pathArr) {
            state.currentPath = pathArr;
        },
        setDicTree (state, dic_tree) {
            state.dicTree = dic_tree;
        }
    }
};

export default Util.merge(common_app, app);
